﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace BYManageInfoSystem
{
    public partial class Frm_TiDanConsignee : DockContent
    {
        public string m_strReturn = "";
        private int m_nMDCountryTabID = 0;
        private DGBY.BLL.MDCountryTab m_bllMDCountryTab = new DGBY.BLL.MDCountryTab();
        private DGBY.BLL.TiDanConsigneeTab m_bllTiDanConsigneeTab = new DGBY.BLL.TiDanConsigneeTab();
        private DataSet m_ds = new DataSet();
        private int m_nSelectPortID = 0;

        public Frm_TiDanConsignee()
        {
            InitializeComponent();
        }
        public Frm_TiDanConsignee(int nMDCountryTabID)
        {
            m_nMDCountryTabID = nMDCountryTabID;
            InitializeComponent();
        }
        private void Frm_TiDanConsignee_Load_1(object sender, EventArgs e)
        {
            DataSet ds = m_bllMDCountryTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxGoodsChnName.DataSource = ds.Tables[0];
                this.comboBoxGoodsChnName.DisplayMember = "国家名称";
                this.comboBoxGoodsChnName.ValueMember = "MDCountryTabID";
                this.comboBoxGoodsChnName.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxGoodsChnName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                if (m_nMDCountryTabID != 0)
                    this.comboBoxGoodsChnName.SelectedValue = m_nMDCountryTabID;
                else 
                    this.comboBoxGoodsChnName.SelectedIndex = 0;
            }
            if (m_nMDCountryTabID != 0)
            {
                ShowAllRecordInDataGridView("TiDanConsigneeTab.MDCountryTabid = '" + m_nMDCountryTabID.ToString() + "'");
            }
            if (Frm_Main.m_strLoginUser == "管理员")
            {
                this.buttonDelPort.Visible = true;
            }
        }
        private void ShowAllRecordInDataGridView(string strWhere)
        {
            ultraGrid1.DataSource = null;
            m_ds = m_bllTiDanConsigneeTab.GetList(strWhere);
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = m_ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns[1].Hidden = true;
            }
        }

        private void buttonShowAllRecordPort_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView("");
        }

        private void buttonAddPort_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.TiDanConsigneeTab modelTiDanConsigneeTab = new DGBY.Model.TiDanConsigneeTab();
                modelTiDanConsigneeTab.TiDanConsigneeName = this.textBoxGoodsEngName.Text;
                modelTiDanConsigneeTab.ReservField1 = "";

                if (this.comboBoxGoodsChnName.SelectedValue != null)
                    modelTiDanConsigneeTab.MDCountryTabID = Convert.ToInt32(this.comboBoxGoodsChnName.SelectedValue.ToString(), 10);

                m_bllTiDanConsigneeTab.Add(modelTiDanConsigneeTab);

                Frm_Main.Log2DB(this.Text, "添加", modelTiDanConsigneeTab.TiDanConsigneeName);
                m_nSelectPortID = m_bllTiDanConsigneeTab.GetMaxId() - 1;
                ShowAllRecordInDataGridView("");
                //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
          
        }

        private void buttonModifyPort_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.TiDanConsigneeTab modelTiDanConsigneeTab = m_bllTiDanConsigneeTab.GetModel(m_nSelectPortID);
                if (modelTiDanConsigneeTab == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                modelTiDanConsigneeTab.TiDanConsigneeName = this.textBoxGoodsEngName.Text;
                modelTiDanConsigneeTab.ReservField1 = "";

                if (this.comboBoxGoodsChnName.SelectedValue != null)
                    modelTiDanConsigneeTab.MDCountryTabID = Convert.ToInt32(this.comboBoxGoodsChnName.SelectedValue.ToString(), 10);

                m_bllTiDanConsigneeTab.Update(modelTiDanConsigneeTab);

                Frm_Main.Log2DB(this.Text, "修改", modelTiDanConsigneeTab.TiDanConsigneeName);
                ShowAllRecordInDataGridView("");
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
        private void cellclick()
        {
            DGBY.Model.TiDanConsigneeTab modelTiDanConsigneeTab = m_bllTiDanConsigneeTab.GetModel(m_nSelectPortID);
            if (modelTiDanConsigneeTab != null)
            {
                this.textBoxGoodsEngName.Text = modelTiDanConsigneeTab.TiDanConsigneeName;
                m_strReturn = modelTiDanConsigneeTab.TiDanConsigneeName;
                if (modelTiDanConsigneeTab.MDCountryTabID != null)
                    this.comboBoxGoodsChnName.SelectedValue = modelTiDanConsigneeTab.MDCountryTabID;
            }
        }

        private void buttonDelPort_Click(object sender, EventArgs e)
        {
            DGBY.Model.TiDanConsigneeTab modelTiDanConsigneeTab = m_bllTiDanConsigneeTab.GetModel(m_nSelectPortID);

            if (modelTiDanConsigneeTab == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllTiDanConsigneeTab.Delete(m_nSelectPortID);
            ShowAllRecordInDataGridView("");
            this.textBoxGoodsEngName.Text = "";

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        //private void dataGridViewPortList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        //{
        //    string strPortID = this.dataGridViewPortList.Rows[this.dataGridViewPortList.CurrentCell.RowIndex].Cells[0].Value.ToString();
        //    m_nSelectPortID = Convert.ToInt32(strPortID, 10);
        //    cellclick();
        //}

        //private void dataGridViewPortList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        //{
        //    {
        //        using (SolidBrush b = new SolidBrush(dataGridViewPortList.RowHeadersDefaultCellStyle.ForeColor))
        //        {
        //            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
        //        }
        //    }
        //}

        private void comboBoxGoodsChnName_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string str = this.comboBoxGoodsChnName.SelectedValue.ToString();
                this.ultraGrid1.DataSource = null;
                //int nID = Convert.ToInt32(str, 10);
                ShowAllRecordInDataGridView("MDCountryTab.MDCountryTabID = '" + str + "'");
            }
            catch { }
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.InitUltraWinGridRowNumSetting(e);
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            string strBYCompID = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells[0].Text;
            m_nSelectPortID = Convert.ToInt32(strBYCompID, 10);
            cellclick();
        }

        private void buttonReturn_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

      


 
    }
}
